<template>
  <div class="rankingBox">
    <div class="item">
      <div class="top">
        <p class="title">官方榜</p>
        <rankingBox
          v-for="(item, index) in officialListDetail"
          :key="index"
          :officialListDetailItem="item"
          @getSongIdAndIndex="getSongIdAndIndex"
          @handleMoreClick="handleMoreClick"
        ></rankingBox>
      </div>
      <div class="global" v-if="officialListDetail">
        <p class="title">全球榜</p>
        <div class="globalItem">
          <songItem
            v-for="(item, index) in globalList"
            :key="index"
            :listCardData="item"
            @listCardDataClick="listCardDataClick"
          ></songItem>
        </div>
        <el-backtop target=".global"></el-backtop>
      </div>
      <el-backtop target=".item"></el-backtop>
    </div>
  </div>
</template>
<script>
import rankingBox from './components/rankingBox.vue'
import songItem from './components/songItem.vue'
import { getAllMusicList, getRankingListDetail } from '@/api/discover/ranking'
import { handleMusicTime } from '@/plugins/utils'
export default {
  name: 'rankingPage',
  data() {
    return {
      officialList: [], // 所有榜单前四个
      officialListDetail: [], // 前四个榜单详细信息
      globalList: [] // 所有榜单,除了前四个
    }
  },
  components: {
    rankingBox,
    songItem
  },
  async created() {
    await this.getALlRankingList()
    // 根据前四个榜单id获取详细信息
    this.officialList.forEach((item) => {
      this.getRankingListDetail(item.id)
    })
    // console.log(this.officialListDetail)
  },
  methods: {
    // 获取所有榜单
    async getALlRankingList() {
      const res = await getAllMusicList()
      this.officialList = res.data.list.slice(0, 4)
      this.globalList = res.data.list.slice(4)
      // console.log(this.globalList)
    },
    // 获取榜单详细信息
    async getRankingListDetail(id) {
      let res = await getRankingListDetail(id)
      res = res.data.playlist
      // console.log(res)
      // 时间格式化分：秒
      res.tracks.forEach((item, index) => {
        res.tracks[index].dt = handleMusicTime(item.dt)
      })
      // console.log(res)
      this.officialListDetail.push(res)
    },
    // 双击播放
    getSongIdAndIndex({ id, index }) {
      // console.log(id, index)
      // 第几个歌单songIndex
      let songIndex = this.officialListDetail.findIndex(
        (item) => item.id === id
      )
      // console.log(songIndex)
      this.$store.commit(
        'updateMusicId',
        this.officialListDetail[songIndex].tracks[index].id
      )
      if (id !== this.$store.state.musicListId) {
        this.$store.commit('updateMusicList', {
          musicList: this.officialListDetail[songIndex].tracks,
          musicListId: id
        })
      }
    },
    // 跳转歌单
    listCardDataClick(id) {
      // console.log(id)
      this.$router.push({ path: '/index/musicDateil', query: { id } })
    },
    handleMoreClick(id, itemId) {
      // console.log(id, itemId)
      this.$router.push({ path: '/index/musicDateil', query: { id: itemId } })
    }
  }
}
</script>
<style lang="scss" scoped>
.rankingBox {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  width: 1200px;
  margin: 0 auto;
  justify-content: center;
  // overflow: hidden;
}
.top {
  width: 100%;
  margin-top: 20px;
  .title {
    font-size: 20px;
    color: #666;
  }
}
.globalItem {
  display: flex;
  flex-wrap: wrap;
  padding-left: 15px;
  .songItem {
    width: 18%;
    margin-right: 15px;
    margin-bottom: 20px;
    cursor: pointer;
    &:nth-child(5n) {
      margin-right: 0;
    }
  }
}
.global {
  .title {
    margin-bottom: 20px;
    font-size: 20px;
    color: #666;
  }
}
.item {
  height: 77vh;
  overflow-x: hidden;
}
.item::-webkit-scrollbar {
  display: none;
}
</style>
